#!/usr/bin/python3.8.9
# -*- coding: utf-8 -*-

# @Author  : Tina Yu
# @Time    : 2022-5-4 12:00
from homework.task_api.base.we_cowork import WeCowork
from homework.task_api.utils_api.read_file import ReadFile


class Department(WeCowork):

    def __init__(self):
        """初始化，获取corp信息，并获取token"""
        yaml_data = ReadFile.read_yaml('corp_info')
        corp_id = yaml_data.get('corp_id').get('manmiaoxuan')
        corp_secret = yaml_data.get('corp_secret').get('contactsApi')
        self.access_token = self.get_access_token(corp_id, corp_secret)

    def create(self, data):
        """新增部门"""
        create_url = '/department/create'
        params = {'access_token': self.access_token}
        r = self.send('POST', url=create_url, params=params, json=data)
        return r

    def update(self, data):
        """修改"""
        update_url = '/department/update'
        params = {'access_token': self.access_token}
        r = self.send('POST', url=update_url, params=params, json=data)
        return r

    def search(self):
        """查找"""
        search_url = '/department/list'
        params = {'access_token': self.access_token}
        r = self.send('GET', search_url, params=params)
        return r

    def delete(self, department_id):
        """删除"""
        delete_url = '/department/delete'
        params = {
            'access_token': self.access_token,
            'id': department_id,
        }
        r = self.send('GET', url=delete_url, params=params)
        return r

    def is_in_department_list(self, department_id):
        """根据部门id是否在列表中来做断言"""
        r = self.search()
        department_ids = self.my_jsonpath(r.json(), '$..id')
        if department_ids:
            return department_id in department_ids
        else:
            return False
